<?php
/**
 * User: 五哥派
 * Date: 2024/6/30
 * Tel(同WX): 17335592719
 */
namespace app\admin\service;

use app\admin\model\store\Goods as GoodsModel;
use app\admin\service\GoodsSku as GoodsSkuService;

class Goods extends BaseService{
    protected $model = null;

    public function __construct(){
        $this->model = new GoodsModel();
    }

    /**
     * 新增商品逻辑
     * @param $data
     * @return array|bool|int|mixed
     * @throws \Exception
     * @throws \think\exception\PDOException
     */
    public function insertData($data) {

        // 开启一个事务
        $this->model->startTrans();
        try {
            $goodsId = $this->add($data);
            if (!$goodsId) {
                return $goodsId;
            }

            // 如果是 统一规格
            if ($data['specs_type'] == 1) {
                $goodsSkuData = [
                    "goods_id" => $goodsId,
                    "category_id" => $data['category_id'],
                    //"specs_value_ids" => 0,
                    "sell_price" => $data['sell_price'],
                    "market_price" => $data['market_price'],
                    "stock" => $data['stock'],
                    "is_default" => 0  //默认
                ];

                $sku_id = (new GoodsSkuService())->add($goodsSkuData);

                // 如果不为0
                if ($sku_id != 0) {
                    $goodsUpdateData = [
                        "sku_id" => $sku_id,
                    ];
                    $goodsRes = $this->model->updateById($goodsId, $goodsUpdateData);
                    if (!$goodsRes) {
                        throw  new \think\Exception("insertData:goods主表更新失败");
                    }
                } else {
                    throw new \think\Exception("sku表新增失败");
                }

            }
            elseif ($data['specs_type'] == 2) {

                $data['goods_id'] = $goodsId;
                $res = (new GoodsSkuService())->saveAll($data);
                // 如果不为空
                if (!empty($res)) {
                    // 总库存
                    $stock = array_sum(array_column($res, "stock"));
                    $goodsUpdateData = [
                        "sell_price" => $res[0]['sell_price'],
                        "market_price" => $res[0]['market_price'],
                        "stock" => $stock,
                        "sku_id" => $res[0]['id'],
                    ];
                    $goodsRes = $this->model->updateById($goodsId, $goodsUpdateData);
                    if (!$goodsRes) {
                        throw  new \think\Exception("insertData:goods主表更新失败");
                    }
                } else {
                    throw new \think\Exception("sku表新增失败");
                }
            }
            // 事务提交
            $this->model->commit();
            return true;
        }catch (\think\Exception $e) {
            // 记录日志 untodo
            // 事务回滚
            $this->model->rollback();
            return false;
        }

    }

    /**
     * 更新商品逻辑
     * @param $data
     * @return array|bool|int|mixed
     * @throws \Exception
     * @throws \think\exception\PDOException
     */
    public function updateData($data) {

        // 开启一个事务
        $this->model->startTrans();
        try {
            $res = $this->update($data);
            if (!$res) {
                return $res;
            }

            $goodsId = $data['id'];
            // 如果是 统一规格
            if ($data['specs_type'] == 1) {
                $goodsSkuData = [
                    "goods_id" => $goodsId,
                    "category_id" => $data['category_id'],
                    //"specs_value_ids" => 0,
                    "sell_price" => $data['sell_price'],
                    "market_price" => $data['market_price'],
                    "stock" => $data['stock'],
                ];
                (new GoodsSkuService())->delete(['goods_id' => $goodsId]);
                $ress = (new GoodsSkuService())->add($goodsSkuData);
                // 如果不为空
                if (!empty($ress)) {
                    // 总库存
                    $goodsUpdateData = [
                        "sku_id" => $ress,
                    ];
                    $goodsRes = $this->model->updateById($goodsId, $goodsUpdateData);
                    if (!$goodsRes) {
                        throw  new \think\Exception("insertData:goods主表更新失败");
                    }
                } else {
                    throw new \think\Exception("sku表新增失败a");
                }

            } elseif ($data['specs_type'] == 2) { // 多规格他是我们电商的核心
                $data['goods_id'] = $goodsId;
                (new GoodsSkuService())->delete(['goods_id' => $goodsId]);
                $res = (new GoodsSkuService())->saveAll($data);
                // 如果不为空
                if (!empty($res)) {
                    // 总库存
                    $stock = array_sum(array_column($res, "stock"));
                    $goodsUpdateData = [
                        "sell_price" => $res[0]['sell_price'],
                        "market_price" => $res[0]['market_price'],
                        "stock" => $stock,
                        "sku_id" => $res[0]['id'],
                    ];
                    $goodsRes = $this->model->updateById($goodsId, $goodsUpdateData);
                    if (!$goodsRes) {
                        throw  new \think\Exception("insertData:goods主表更新失败");
                    }
                } else {
                    throw new \think\Exception("sku表新增失败");
                }
            }
            // 事务提交
            $this->model->commit();
            return true;
        }catch (\think\Exception $e) {
            // 记录日志 untodo
            // 事务回滚
            $this->model->rollback();
            return false;
        }
    }
}